package com.bgba.oer.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.Asset;
import com.flashline.registry.openapi.entity.AssetSummary;
import com.flashline.registry.openapi.entity.AssetType;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.Contact;
import com.flashline.registry.openapi.entity.RelationshipType;
import com.flashline.registry.openapi.query.AssetCriteria;
import com.flashline.registry.openapi.query.AssetTypeCriteria;
import com.flashline.registry.openapi.query.ContactCriteria;
import com.flashline.registry.openapi.query.SearchTerm;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;

public class AssetSystemService {


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("======================== ");
		String urlConnectionLogin = "http://desasoa01/oer/services/FlashlineRegistry?WSDL";
		String username = " L0269239";
		String password = "finde2011";
		/*
		String urlConnectionLogin = "http://172.17.1.10:8101/oer/services/FlashlineRegistry?WSDL";
		String username = " admin";
		String password = "admin123";
		*/
		
		AssetSystemService instance = new AssetSystemService();
		//instance.addRelationships(urlConnectionLogin, username, password);
		
		try {		///////////////////////////////////////////////////////////
		// Connect to Oracle Enterprise Repository
		///////////////////////////////////////////////////////////
		URL lURL = null;
		lURL = new URL(urlConnectionLogin);
		FlashlineRegistry repository = new FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);

		///////////////////////////////////
		// Login to OER
		///////////////////////////////////
		AuthToken authToken = repository.authTokenCreate(username, password);
		
		instance.searchAssetByName(repository, authToken, "BasicSystemForMigration");

		instance.addSystemAsset(repository, authToken, null);

		//addRelationships(urlConnectionLogin, username, password);
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}

	public static Asset addSystemAsset(FlashlineRegistry repository, AuthToken authToken, Asset systemAsset) throws OpenAPIException, RemoteException{
		
		AssetTypeCriteria assetTypeCriteria = new AssetTypeCriteria();
		String assetTypeNameCriteria = "System";
		assetTypeCriteria.setNameCriteria(assetTypeNameCriteria);
		AssetType[] assetTypes = repository.assetTypeQuery(authToken, assetTypeCriteria);
		AssetType systemAssetType = null;
		if (assetTypes != null && assetTypes.length == 1) {
			systemAssetType = assetTypes[0];		
		} else if (assetTypes != null && assetTypes.length > 1) {
			for (AssetType assetType : assetTypes) {
				System.out.println("========= assetType.getID="+assetType.getID());
				System.out.println("========= assetType.getName="+assetType.getName());
			}
			System.out.println("======================================= WARN!!! FOUND more than one assetType with name="+assetTypeNameCriteria + ", assets resultSet="+assetTypes.length);
			String[] messageArgs = {"Error, Asset Type duplicated"};
			throw new OpenAPIException(messageArgs, 1, "", "", "ERROR" );
			
		} else {
			System.out.println("======================================= WARN!!! NOT FOUND assetType with name="+assetTypeNameCriteria + ", assets resultSet=0");
			String[] messageArgs = {"Error, NOT Found Asset Type"};
			throw new OpenAPIException(messageArgs, 1, "", "", "ERROR" );			
		}
		
		System.out.println("================== Using systemAssetType.getID="+systemAssetType.getID());
		System.out.println("================== Using systemAssetType.getName="+systemAssetType.getName());
		System.out.println("================== Using systemAssetType.getDisplayName="+systemAssetType.getDisplayName());
		
		Asset newSystemAsset = repository.assetCreate(authToken, systemAsset.getName(), systemAsset.getVersion(), systemAssetType.getID());		
		
		return newSystemAsset;
		//return null;		
	}
	
	public static Asset searchAssetByName(FlashlineRegistry repository, AuthToken authToken, String assetNameCriteria) throws RemoteException, OpenAPIException {
		System.out.println("\n ######################################################");
		////////////////////////////////////////////////////////////
		// Search assets
		// try a general search which includes Name, version,
		// description, and keywords
		////////////////////////////////////////////////////////////

		AssetCriteria criteria = new AssetCriteria();
		criteria = new AssetCriteria();
		criteria.setGeneralCriteria(assetNameCriteria);

		AssetSummary[] assets = repository.assetQuerySummary(authToken, criteria);
		//criteria.
		System.out.println("======================================= assets="+assets);
		System.out.println("======================================= assets.length="+assets.length);
		Asset myAsset = null;
		if (assets.length == 1){
			AssetSummary assetSummary = assets[0];
			//System.out.println("========================= assetSummary.getID="+assetSummary.getID());
			//System.out.println("========================= assetSummary.getName="+assetSummary.getName());
			//System.out.println("========================= assetSummary.getVersion="+assetSummary.getVersion());
			//System.out.println("========================= assetSummary.getTypeID="+assetSummary.getTypeID());

			myAsset = repository.assetRead(authToken, assetSummary.getID());

			System.out.println("========================= myAsset.getID="+myAsset.getID());
			System.out.println("========================= myAsset.getName="+myAsset.getName());
			System.out.println("========================= myAsset.getDisplayName="+myAsset.getDisplayName());
			System.out.println("========================= myAsset.getLongName="+myAsset.getLongName());
			System.out.println("========================= myAsset.getTypeName="+myAsset.getTypeName());

			if (myAsset.getContacts() != null && myAsset.getContacts().length > 0) {
				for (Contact contact : myAsset.getContacts()) {
					System.out.println("============== contact.getID="+contact.getID());
					System.out.println("============== contact.getName()="+contact.getName());
					System.out.println("============== contact.getPhoneNumber()="+contact.getPhoneNumber());
					System.out.println("============== contact.getState()="+contact.getState());
					System.out.println("============== contact.getType()="+contact.getType());
				}
			}
			
			/*
			ContactCriteria contactCriteria = new ContactCriteria();
			contactCriteria.setNameCriteria("Busso");
			Contact[] contacts = repository.contactQuery(authToken, contactCriteria);

			for (Contact contact : contacts) {
				System.out.println("============== contact.getID="+contact.getID());
				System.out.println("============== contact.getName()="+contact.getName());
				System.out.println("============== contact.getPhoneNumber()="+contact.getPhoneNumber());
				System.out.println("============== contact.getState()="+contact.getState());
				System.out.println("============== contact.getType()="+contact.getType());
			}
			*/
			
			
			/*
			Contact contact = new Contact();
			contact.set
			*/

		} else if (assets != null  && assets.length > 1){
			System.out.println("======================================= WARM FOUND  duplicated assets with name="+assetNameCriteria + ", contact resultSet="+assets.length);
			String[] messageArgs = {"Error, Contact duplicated"};
			throw new OpenAPIException(messageArgs, 1, "", "", "ERROR" );

		} else if (assets == null || assets.length == 0){
			System.out.println("======================================= NOT FOUND assets with name="+assetNameCriteria + ", assets resultSet=0");				
		}			
		

		return myAsset;
	}

}
